import http from '../utils/http';

const BASE_URL = 'http://localhost:8082'; // 后端API地址

/**
 * 获取所有会议室列表
 * @returns {Promise<AjaxResult>} 返回会议室列表数据
 * @typedef {Object} AjaxResult
 * @property {number} code - 状态码 200成功，500失败
 * @property {string} msg - 提示信息
 * @property {Array<Room>} data - 会议室数据
 */
export const getAllRooms = async () => {
  try {
    const response = await http.get('/rooms');
    return response.data;
  } catch (error) {
    throw error;
  }
};

/**
 * 创建新会议室
 * @param {Object} roomData - 会议室数据
 * @param {string} roomData.roomname - 会议室名称
 * @param {number} roomData.roomType - 会议室类型 (1:教室, 2:圆桌, 3:董事会, 4:多媒体)
 * @param {number} roomData.capacity - 容纳人数
 * @param {number} roomData.projector - 投影仪状态 (0:异常, 1:正常)
 * @param {number} roomData.sound - 音响状态 (0:异常, 1:正常)
 * @param {number} roomData.internet - 网络状态 (0:异常, 1:正常)
 * @param {number} roomData.price - 价格/小时
 * @param {number} roomData.status - 状态 (0:未使用, 1:使用中)
 * @returns {Promise<AjaxResult>} 返回创建结果
 */
export const createRoom = async (roomData) => {
  try {
    const response = await http.post('/rooms', roomData);
    return response.data;
  } catch (error) {
    throw error;
  }
};

/**
 * 更新会议室信息
 * @param {number} roomId - 会议室ID
 * @param {Object} roomData - 会议室更新数据
 * @returns {Promise<AjaxResult>} 返回更新结果
 */
export const updateRoom = async (roomId, roomData) => {
  try {
    const response = await http.put(`/rooms/${roomId}`, roomData);
    return response.data;
  } catch (error) {
    throw error;
  }
};

/**
 * 删除会议室
 * @param {number} roomId - 会议室ID
 * @returns {Promise<AjaxResult>} 返回删除结果
 */
export const deleteRoom = async (roomId) => {
  try {
    const response = await http.delete(`/rooms/${roomId}`);
    return response.data;
  } catch (error) {
    throw error;
  }
};

/**
 * 获取单个会议室详情
 * @param {number} roomId - 会议室ID
 * @returns {Promise<AjaxResult>} 返回会议室详情
 */
export const getRoomById = async (roomId) => {
  try {
    const response = await http.get(`/rooms/${roomId}`);
    return response.data;
  } catch (error) {
    throw error;
  }
};

/**
 * 根据条件筛选会议室
 * @param {Object} searchCriteria - 筛选条件
 * @param {number} [searchCriteria.capacity] - 容纳人数
 * @param {number} [searchCriteria.projector] - 投影仪状态 (0:异常, 1:正常)
 * @param {number} [searchCriteria.sound] - 音响状态 (0:异常, 1:正常)
 * @param {number} [searchCriteria.internet] - 网络状态 (0:异常, 1:正常)
 * @param {string} [searchCriteria.startTime] - 开始时间 (ISO 8601 格式)
 * @param {string} [searchCriteria.endTime] - 结束时间 (ISO 8601 格式)
 * @returns {Promise<AjaxResult>} 返回符合条件的会议室列表数据
 */
export const searchRooms = async (searchCriteria) => {
  try {
    const response = await http.post('/rooms/search', searchCriteria);
    return response.data;
  } catch (error) {
    throw error;
  }
};
